import Sequelize from 'sequelize';
import express from 'express';
import { statusList, sequelizeStatus } from '../../common/status'; // 状态码对照
import { Item, Sku } from '../../modal/index'; // 数据库实体类
import { checkPermission } from '../../utils/utils'; // 工具类

const router = express.Router();
const permission = ["all"];

router.post('/', async function (Request, Response, next) {
  // Item.Modal.sync()
  //权限校验
  await checkPermission(Request, Response, permission)
  let param = Request.body;
  let querySql = {
    [Sequelize.Op.and]: []
  }
  param.id && querySql[Sequelize.Op.and].push({
    id: {
      [Sequelize.Op.eq]: param.id
    }
  })
  Item.Modal.findAndCountAll({
    where: querySql,
    offset: (param.pageNo - 1) * param.pageSize,
    limit: param.pageSize, // 分页参数
    include: [{
      model: Sku.Modal,
      as: 'skus'
    }], // 表关联
    distinct: true //去重，表关联时需要
  }).then(res => {
    Response.json({
      data: {
        list: res.rows,
        total: res.count,
      },
      message: "success"
    });
  }).catch(err => {
    console.log(err)
    Response.status(500).send({ message: sequelizeStatus[err.name] || statusList[500] || err.name });
  });
});

module.exports = router;
  // 创建新记录
  // for (let i = 0; i <= 50; i++) {
  //     Item.Modal.create(
  //     {
  //       itemName: "雪佛兰护肤保湿凝胶补血颗粒" + String(i),
  //       itemShelf: 0,
  //       itemMainImg: "https://logistics-2b-dev.oss-cn-hangzhou.aliyuncs.com/wahaha.jpg",
  //       price: 18.24,
  //     }
  //   );
  // }
  //  for (let i = 0; i <= 3; i++) {
  //   Sku.Modal.create(
  //     {
  //       skuName: "颜色",
  //       skuAtt: "白色" + String(i),
  //       itemId: 5,
  //       stockQuantity: 18 + i,
  //       price: 12.18 + i,
  //       outCode: "ZRWANFK013" + String(i),
  //     }
  //   );
  // }